/************************************
*  Copyright (C) 2010 Julien MOTTIN.
*  Contact: julien.mottin@gueydan.eu
*
*  This file is part of Gueydan.
*
*  Gueydan is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  Gueydan is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with Gueydan.  If not, see <http://www.gnu.org/licenses/>.
*
*************************************/
// local
#include "mainframe.h"
#include "uistring.h"

// QtGui
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QFormLayout>
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
#include <QDateEdit>
#include <QDoubleSpinBox>
#include <QFileDialog>
#include <QPixmap>

// QtCore
#include <QDate>


// Standard libraries
#include <stddef.h>

MainFrame::MainFrame(QWidget *pParent)
    :QFrame(pParent,0)
{
    // Properties
    setFrameShape(QFrame::StyledPanel);
    setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);

    // Horizontal layout of main Frame
    QHBoxLayout *mfLayout = new QHBoxLayout(this);
    mfLayout->addWidget(createBatchFrame());
    mfLayout->addWidget(createIngredientsFrame());
    mfLayout->addWidget(createStyleFrame());
    setLayout(mfLayout);

    // Set actions
    createActions();
}

MainFrame::~MainFrame()
{
}

QSize MainFrame::sizeHint() const
{
    return QSize(600,400);
}

void MainFrame::onSelectPicture()
{
    // choose picture
    QString imagePath = QFileDialog::getOpenFileName(this,BF_Image,"", BF_ImageExtensions);
    qDebug("chosen file : %s",imagePath.toAscii().constData());

    // load it
    QPixmap image(imagePath);
    _imageLabel->setPixmap(image);

    // dispatch notif
    emit batchInfoChanged();
}

QFrame * MainFrame::createBatchFrame()
{
    // Create frame
    QFrame *batchFrame = new QFrame(this,0);
    batchFrame->setMaximumWidth(400);
    batchFrame->setFrameShape(QFrame::StyledPanel);

    // Batch Frame
    _imageLabel = new QLabel(batchFrame);
    _imageLabel->setFrameShape(QFrame::StyledPanel);
    _imageLabel->setScaledContents(true);
    _imageButton = new QPushButton(BF_Browse,batchFrame);
    _nameEdit = new QLineEdit();
    QDate today = QDate::currentDate();
    _dateEdit = new QDateEdit(today);
    _brewerEdit = new QLineEdit();
    _efficiencyEdit = new QDoubleSpinBox();
    _efficiencyEdit->setDecimals(1);
    _efficiencyEdit->setSingleStep(0.5);
    _efficiencyEdit->setValue(80.0);
    _efficiencyEdit->setMinimum(0.0);
    _efficiencyEdit->setMaximum(100.0);
    _ogSpinBox = new QDoubleSpinBox();
    _ogSpinBox->setDecimals(3);
    _ogSpinBox->setValue(1.060);
    _ogSpinBox->setMinimum(1.0);
    _ogSpinBox->setMaximum(1.2);
    _ogSpinBox->setSingleStep(0.001);
    _fgSpinBox = new QDoubleSpinBox();
    _fgSpinBox->setDecimals(3);
    _fgSpinBox->setValue(1.012);
    _fgSpinBox->setMinimum(1.0);
    _fgSpinBox->setMaximum(1.2);
    _fgSpinBox->setSingleStep(0.001);

    QFrame *formFrame = new QFrame(batchFrame);
    QFormLayout *formLayout = new QFormLayout(formFrame);
    formLayout->addRow(BF_Name,_nameEdit);
    formLayout->addRow(BF_Date,_dateEdit);
    formLayout->addRow(BF_Brewer,_brewerEdit);
    formLayout->addRow(BF_Efficiency,_efficiencyEdit);
    formLayout->addRow(BF_OG,_ogSpinBox);
    formLayout->addRow(BF_FG,_fgSpinBox);
    formFrame->setLayout(formLayout);

    // Vertical layout of batch frame
    QVBoxLayout *bfLayout = new QVBoxLayout(batchFrame);
    bfLayout->addWidget(_imageLabel);
    bfLayout->addWidget(_imageButton);
    bfLayout->addWidget(formFrame);
    batchFrame->setLayout(bfLayout);

    return batchFrame;
}

QFrame *MainFrame::createIngredientsFrame()
{
    // instantiate frame
    QFrame *ingredientsFrame = new QFrame(this,0);
    ingredientsFrame->setFrameShape(QFrame::StyledPanel);

    return ingredientsFrame;
}

QFrame * MainFrame::createStyleFrame()
{
    // instantiate frame
    QFrame *styleFrame = new QFrame(this,0);
    styleFrame->setFrameShape(QFrame::StyledPanel);

    return styleFrame;
}

void MainFrame::createActions()
{
    // Triggers batch frame modifications
    connect(_nameEdit,SIGNAL(textEdited(QString)),SIGNAL(batchInfoChanged()));
    connect(_dateEdit,SIGNAL(dateChanged(QDate)),SIGNAL(batchInfoChanged()));
    connect(_brewerEdit,SIGNAL(textEdited(QString)),SIGNAL(batchInfoChanged()));
    connect(_efficiencyEdit,SIGNAL(valueChanged(double)),SIGNAL(batchInfoChanged()));
    connect(_ogSpinBox,SIGNAL(valueChanged(double)),SIGNAL(batchInfoChanged()));
    connect(_fgSpinBox,SIGNAL(valueChanged(double)),SIGNAL(batchInfoChanged()));

    // Open Dialog box for picture choice
    connect(_imageButton,SIGNAL(released()),SLOT(onSelectPicture()));
}
